﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GameNetworking;
using System.Net;

namespace GameServer
{
    public partial class ClientForm : Form
    {
        User user;

        private delegate void MessageReceivedCallback(string strMessage);

        // Needed to update the form with messages from another thread
        //private delegate void UpdateChatCallback(string strMessage);
        //// Needed to set the form to a "disconnected" state from another thread
        //private delegate void CloseConnectionCallback(string strReason);


        public ClientForm(User c)
        {
            InitializeComponent();
            user = c;
            tbHandle.Text = c.UserName;
            tbServer.Text = c.Server.ToString();
            if (c.Connected)
                pnlConnectionStatus.BackColor = Color.Green;
            Application.ApplicationExit += new EventHandler(Application_ApplicationExit);
            User.MessageReceived += new UserMessageReceivedEventHandler(User_MessageReceived);
        }

        void User_MessageReceived(object sender, UserMessageReceivedEventArgs e)
        {
            this.Invoke(new MessageReceivedCallback(this.UpdateChat), new object[] { e.EventMessage });
        }

        private void UpdateChat(string strMessage)
        {
            tbClientChat.AppendText(strMessage + "\r\n");
        }

        void Application_ApplicationExit(object sender, EventArgs e)
        {
            if (user.Connected == true)
            {
                // Closes the connections, streams, etc.
               user.Disconnect();            
            }
        }

        public ClientForm()
        {
            InitializeComponent();
            tbHandle.Text = "Unknown";
            tbPort.Text = "7777";
            tbServer.Text = Dns.GetHostAddresses("localhost")[0].ToString();
            btnSend.Enabled = true;
        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            if (user !=null && user.Connected)
            {
                System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
                user.SendMessage(tbMessage.Text);
                tbMessage.Text = "";
                tbMessage.Select(0, 1);
            }
        }

        private void btnConnect_Click(object sender, EventArgs e)
        {
            try
            {
                if (user != null && !user.Connected)
                {
                    user.Connect();
                    pnlConnectionStatus.BackColor = Color.Green;
                    btnSend.Enabled = true;
                }
                else
                {
                    user = new User(tbServer.Text, Int32.Parse(tbPort.Text), tbHandle.Text);
                    user.Connect();
                    btnSend.Enabled = true;
                    pnlConnectionStatus.BackColor = Color.Green;
                    btnSend.Enabled = true;
                }
            }
            catch (Exception ex)
            {
                 if (ex.Message.Contains("refused"))
                {
                    btnSend.Enabled = false;
                    pnlConnectionStatus.BackColor = Color.Yellow;
                }
                else
                {
                    btnSend.Enabled = false;
                    pnlConnectionStatus.BackColor = Color.Red;
                }
            }

        }

        private void ClientForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            user.Disconnect();
        }

        private void tbMessage_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnSend_Click(this, null);
            }

        }

        private void tbMessage_TextChanged(object sender, EventArgs e)
        {
        }
    }
}
